<?php
/*
 * Created on Oct 25, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("Files.php");							//--> Files


$objFiles	= new Files();		//--> Files

$fileName	= $_GET['fileName'];
$height		= $_GET['height'];
$width		= $_GET['width'];
$dir		= $_GET['dir'];

if(empty($height))
	$height = 0;
	
if(empty($width))
	$width = 0;

$filepath = DIR_IMAGE_HOME . "$dir/$fileName" ;


if(!file_exists($filepath) || !is_file($filepath)){
	$filepath = '../images/noimage.jpg';
}


		
//echo 'path = ' . file_exists($filepath) . '<br>';
//echo 'path = ' . $filepath;
//exit;
RatioAdjuct($filepath, $width, $height, false, true);



function RatioAdjuct($filename = '', $w = 100, $h = 100, $override = false, $background = false, $color = '0xFFFFFF') 
{
	$output = @file_get_contents($filename);
	$etag = md5($output);
			
	if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {  // if client has cached this file
		header('HTTP/1.1 304 Not Modified');        // we just send a 304 header, without any data
		exit;
	}
	
	$imageinfo = getImageSize ( $filename );
	Header("Content-type: " . $imageinfo['mime']);
	
	$imgWidth 	= $imageinfo[0];
	$imgHeight	= $imageinfo[1];
	$imaType	= $imageinfo[2];
	
	if($w <= 0 || $h <=0){
		$w = $imgWidth;
		$h = $imgHeight;
	}
	
	$ratioX = $imgWidth / $w;
	$ratioY = $imgHeight / $h;
	
	if ($ratioX > $ratioY || $ratioX == $ratioY) {
		$dst_w = $w;
		$dst_h = ceil ( $imgHeight / $ratioX );
	} else if ($ratioY > $ratioX) {
		$dst_h = $h;
		$dst_w = ceil ( $imgWidth / $ratioY );
	}
	
	//
	switch ($imaType) {
		case 2 :
			$im = imageCreateFromJpeg ( $filename );
			break;
		case 1 :
			$im = imageCreateFromGif ( $filename );
			break;
		
		case 3 :
			$im = imageCreateFromPng ( $filename );
	}
	
	//
	if ($background) {
		//
		$dec = hexdec ( $color );
		$red = 0xFF & ($dec >> 0x10);
		$green = 0xFF & ($dec >> 0x8);
		$blue = 0xFF & $dec;
		
		//
		$dst_pos = array ('d_x' => 0, 'd_y' => 0 );
		($dst_w == $w) ? ($dst_pos ['d_y'] = (($h - $dst_h) / 2)) : ($dst_pos ['d_x'] = (($w - $dst_w) / 2));
		
		$imBox = imageCreateTrueColor ( $w, $h );
		$color_bg = imageColorAllocate ( $imBox, $red, $green, $blue );
		imageFill ( $imBox, 0, 0, $color_bg );
		imageCopyreSampled ( $imBox, $im, $dst_pos ['d_x'], $dst_pos ['d_y'], 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight );
	} else {
		$imBox = imageCreateTrueColor ( $dst_w, $dst_h );
		imageCopyreSampled ( $imBox, $im, 0, 0, 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight );
	}
	
	//
	if ($override)
		$filename = str_replace ( strrchr ( $filename, '.' ), '', $filename ) . '_thumb.png';
	
//		header("Content-type:" . $imageType);
	header('Etag: ' . $etag);
	header('Cache-Control:max-age=604800');
					
	return imagejpeg ( $imBox, '', 100);
}

?>
